<?php

	//	验证码工具类
	class Captcha{
		private $width;//int
		private $height;
		private $fontsize;
		private $strlen;
		private $pixels;
		private $lines;
        private $minus_x;
        private $minus_y;


		public function __construct($arr=array()){
			$this->width = isset($arr['width'])?$arr['width']:$GLOBALS['config']['captcha']['width'];
			$this->height = isset($arr['height'])?$arr['height']:$GLOBALS['config']['captcha']['height'];
			$this->strlen = isset($arr['strlen'])?$arr['strlen']:$GLOBALS['config']['captcha']['strlen'];
			$this->pixels = isset($arr['pixels'])?$arr['pixels']:$GLOBALS['config']['captcha']['pixels'];
			$this->lines = isset($arr['lines'])?$arr['lines']:$GLOBALS['config']['captcha']['lines'];
			$this->fontsize = isset($arr['fontsize'])?$arr['fontsize']:$GLOBALS['config']['captcha']['fontsize'];
            $this->minus_x = isset($arr['minus_x'])?$arr['minus_x']:$GLOBALS['config']['captcha']['minus_x'];
            $this->minus_y = isset($arr['minus_y'])?$arr['minus_y']:$GLOBALS['config']['captcha']['minus_y'];
		}


	//	产生验证码方法,用对象来调用
	public function createCaptcha(){
		$img = imagecreatetruecolor($this->width,$this->height);
		$bg_color = imagecolorallocate($img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
		imagefill($img,0,0,$bg_color);

		$this->getLines($img);
		$this->getPixels($img);
		$captcha = $this->getCaptcha();
		
		$font_color = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
		$start_x=$this->width/2-$this->minus_x;
		$start_y=$this->height/2-$this->minus_y;

		if(imagestring($img,$this->fontsize,$start_x,$start_y,$captcha,$font_color)){
				header('Content-type:image/png');
				imagepng($img);
		}else{
				return '失败';
		}

	}
	//bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )画线
		private function getLines($img){
			for($i=0;$i<$this->lines;$i++){
				$line_color =  imagecolorallocate($img,mt_rand(150,255),mt_rand(150,255),mt_rand(150,255));
				imageline($img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$line_color);
			}

		}
		//bool imagesetpixel ( resource $image , int $x , int $y , int $color )，	画点
		private function getPixels($img){
			for($i=0; $i<$this->pixels; $i++){
				$pixel_color =  imagecolorallocate($img,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
				imagesetpixel($img,mt_rand(0,$this->width),mt_rand(0,$this->height),$pixel_color);
			}
		}
		//	随机字符串
		private function getCaptcha(){
			 $str = implode('',array_merge(range(1,9),range("a","z"),range("A","Z")));
			$captcha = '';
			for($i=0,$len=strlen($str);$i<$this->strlen;$i++){
				$captcha.=	$str[mt_rand(0,$len-1)]." ";
			}
			$_SESSION['captcha'] = str_replace(' ','',$captcha);
			return $captcha;
		}
        /**
         * 判断用户输入的验证码是否正确
         * @param String $captcha
         * @return bool
         */
        public static function checkCaptcha($captcha){
			return strtolower($captcha) == strtolower($_SESSION['captcha']);
		} 

	}

